package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.mapper.AuthMapper;
import com.situ.feemanagement.mapper.UserMapper;
import com.situ.feemanagement.pojo.entity.Auth;
import com.situ.feemanagement.pojo.entity.User;
import com.situ.feemanagement.pojo.query.AuthQuery;
import com.situ.feemanagement.service.IAuthService;
import com.situ.feemanagement.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements IAuthService {

    @Autowired
    private AuthMapper authMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> selectByPage(AuthQuery authQuery) {
        PageHelper.startPage(authQuery.getPage(), authQuery.getLimit());
        List<Auth> list = authMapper.selectByPage(authQuery);
        PageInfo<Auth> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("list", list);
        return map;
    }

    @Override
    public void deleteById(int id) {
        authMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            authMapper.deleteById(id);
        }
    }

    @Override
    public void add(Auth auth) {
        authMapper.add(auth);
    }

    @Override
    public Auth selectById(int id) {
        return authMapper.selectById(id);
    }

    @Override
    public void Update(Auth auth) {
        authMapper.update(auth);
    }

    @Override
    public void updateStatus(int id, int status) {
        authMapper.updateStatus(id, status);
    }

    @Override
    public List<Auth> selectByRole() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User user = userMapper.selectById(id);
        List<Auth> list =  authMapper.selectByRole(user.getRole());
        for (Auth auth : list) {
            String[] paths = auth.getPath().split("/");
            String path = "/" + paths[2];
            auth.setPath(path);
        }
        return list;
    }
}
